package com.huazi.msmservice.sevice;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;

import com.aliyuncs.profile.DefaultProfile;

import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author shkstart
 * @create 2021-03-27 11:54
 */
@Service
public class MsmServiceImpl implements MsmService{
    @Override
    public boolean sendMsm(Map<String,Object> param, String phone) {
        if(StringUtils.isEmpty(phone)) return false;
        DefaultProfile profile =
                DefaultProfile.getProfile("default","LTAIq6nIPYC9VROj","EQ7UcixT9wEq9F35rORPqKr8xkTF");
                IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType. POST);
        request.setSysDomain(" dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //发送的手机号码
        request.putQueryParameter("phone",phone );
        //申请的签名名称
        request.putQueryParameter("SignName","我的谷粒在线教育网站");
        //模板code
        request.putQueryParameter("TemplateCode","SMS_180051135");
        //验证码数据转换
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //最终发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
